package com.kzd.gtgame.jetpack.manager

import com.kzd.gtgame.app.*
import com.kzd.gtgame.base.BaseManager
import com.kzd.gtgame.entity.*
import com.module.lemlin.http.HttpResponseDataE
import com.module.lemlin.http.HttpResponseDataT
import rxhttp.RxHttp
import rxhttp.await

class SearchRepository : BaseManager() {

    /**
     * 热门搜索
     * */
    suspend fun searchHot(): HttpResponseDataE<SearchHot> =
        RxHttp.postForm(APP_HOT_SO)
            .await()

    /**
     * 搜索热门-猜你想搜-列表
     * */
    suspend fun searchList(): HttpResponseDataE<SearchDefault> {
        val response = RxHttp.postForm(NEW_APPS_SEARCH_LIST)
            .await<HttpResponseDataT<SearchHotGuess>>()
        val data = response.data
        val defaultList = mutableListOf<SearchDefault>()
        if (!data.hot.isNullOrEmpty()) defaultList.add(
            SearchDefault(name = "热搜游戏", list = data.hot)
        )
        if (!data.guess.isNullOrEmpty()) defaultList.add(
            SearchDefault(name = "猜你想搜", list = data.guess)
        )
        return HttpResponseDataE(data = defaultList).apply {
            message = response.message
            code = response.code
        }
    }

    /**
     * 搜索结果信息
     *
     * @param keyword
     * */
    suspend fun searchInfo(keyword: String): HttpResponseDataE<GameBean> =
        RxHttp.postForm(APP_SEARCH_INFO)
            .add("keyword", keyword)
            .await()

    /**
     * 搜索结果点击
     *
     * @param gameId 游戏id
     * */
    suspend fun searchClick(gameId: String): SearchClickResponse =
        RxHttp.postForm(NEW_APPS_SEARCH_DETAIL_CLICK)
            .add("game_id", gameId)
            .await()
}